//
//  RootViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-5-17.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "RootViewController.h"

#import "ViewControllerChooser.h"


#import "DataManager.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}



#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 0;
	switch (section) {
		case 0:
			ret = 2; // TODO 注销，上传
			break;
		case 1:
			ret = 1;
			break;
	}
    return ret;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		// 针对不同的目标平台进行适配
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	DataManager *dm = [DataManager sharedDataManager];
	authorized = [dm isAuthorized];

    switch (indexPath.section) {
		case 0:
			if(authorized){
				if(indexPath.row == 0){
					// TODO 授权 对于未登录的 显示 login 对于登录的，显示 logout 显示需要根据数据配置
					cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"LOGOUT",@"main",@""))];
					cell.accessoryType = UITableViewCellAccessoryNone;
				}
			}else{
				if(indexPath.row == 0){
					// TODO 授权 对于未登录的 显示 login 对于登录的，显示 logout 显示需要根据数据配置
					cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"LOGIN",@"main",@""))];
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				}
			}
				if(indexPath.row == 1){
					// TODO 上传
					cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"UPLOAD",@"main",@""))];
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					
				}else if(indexPath.row == 2){
					// TODO 针对照片集
					cell.textLabel.text = @"PhotoSet";
				}
			
			break;
		case 1:
			cell.textLabel.text = [NSString stringWithString:NSLocalizedStringFromTable(@"ABOUT_IFLICK",@"main",@"")];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			break;
		default:
			break;
	}
	
	return cell;
}




// Override to support row selection in the table view.
/**
 * 执行相应操作
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	
	
	UIViewController *next = nil;
	// DataManager *dm = [DataManager sharedDataManager];
	
	switch (indexPath.section) {
		case 0:
			if(indexPath.row == 0){
				//authorized = [dm isAuthorized];
//				if(!authorized){
//				
//					// TODO 授权
//					next = [[ViewControllerChooser sharedChooser] choose:VCC_ID_AUTH];
//				}else{
//					
//					// TODO 增加针对注销的提示处理。
//					// 询问是否要进行注销操作
//					NSString *aTitle = (NSLocalizedStringFromTable(@"CONFIRM",@"main",@""));
//					NSString *aMessage = (NSLocalizedStringFromTable(@"WANT_LOGOUT",@"main",@""));
//					NSString *aCancel = (NSLocalizedStringFromTable(@"CANCEL",@"main",@""));
//					NSString *aOK = (NSLocalizedStringFromTable(@"OK",@"main",@""));
//					UIAlertView *logoutAlert = [[UIAlertView alloc] initWithTitle:aTitle message:aMessage delegate:self cancelButtonTitle:aCancel otherButtonTitles:aOK,nil];
//					[logoutAlert show];
//					[logoutAlert release];
//				}
			}else if(indexPath.row == 1){
				// TODO 上传
				next = [[ViewControllerChooser sharedChooser] choose:VCC_ID_UPLOAD];
			}
			break;
		case 1:
			// TODO 关于界面
			next = [[ViewControllerChooser sharedChooser] choose:VCC_ID_ABOUT];
			break;
		default:
			break;
	}
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	if(next != nil){
		[self.navigationController pushViewController:next animated:YES];
	}
	
}




// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	if(buttonIndex == 1){
		// TODO 目前此方法的 alertView只有一个，因此直接调用相应的功能
		// TODO 执行注销功能
		DataManager *dm = [DataManager sharedDataManager];
		[dm logout];
		authorized = NO;
		[self.tableView reloadData];
	}	
}


- (void)dealloc {
    [super dealloc];
}


@end

